<?php
	session_start();
	if (!isset($_GET[u1]) || !isset($_GET[u2]))
	{
		echo 'Sepertinya anda salah halaman';
	}
	else if (!isset($_SESSION['username']))
	{
		echo 'Login dulu ya...';
	}
	else if (strcmp($_SESSION['username'],$_GET[u1])!=0)
	{
		echo 'Anda tidak boleh melihat halaman ini...';
	}
	else
	{
		require_once 'php/database.php';
		$hasil = database::query("SELECT username, Profpic FROM user WHERE username='".$_GET[u1]."' OR username='".$_GET[u2]."'");
		if (mysql_num_rows($hasil)<2)
			echo 'Halamannya tidak valid...';
		else
		{
			$baris1 = mysql_fetch_array($hasil);
			$baris2 = mysql_fetch_array($hasil);
			if ($baris1[0] == $_GET[u2])
			{
				$baris3 = $baris1;
				$baris1 = $baris2;
				$baris2 = $baris3;
			}
			if ($baris1[1]==NULL)
				$string1 = 'images/bg-photo.png';
			else
			{
				$hasil2 = database::query("SELECT filefoto FROM foto WHERE IDFoto='$baris1[1]'");
				$baris3 = mysql_fetch_array($hasil2);
				$string1 = 'users/'.$baris1[0].'/'.$baris3[0];
			}
			if ($baris2[1]==NULL)
				$string2 = 'images/bg-photo.png';
			else
			{
				$hasil2 = database::query("SELECT filefoto FROM foto WHERE IDFoto='$baris2[1]'");
				$baris3 = mysql_fetch_array($hasil2);
				$string2 = 'users/'.$baris2[0].'/'.$baris3[0];
			}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style/footoo.css" />
<link rel="stylesheet" type="text/css" href="style/ftheader.css" />
<link rel="stylesheet" type="text/css" href="style/ftfooter.css" />
<link rel="stylesheet" type="text/css" href="style/ftcommoncontent.css" />
<script type="text/javascript" src="registrasi.js"></script>
<script type="text/javascript" src="datetimepicker.js"></script>
<script type="text/javascript" src="search.js"></script> 
<script type="text/javascript" src="visopt.js"></script> 
<title>Foo-Too</title>
</head>

<body onload = "login()">
    <div id="headerwrapper">
		<div id = "headerbgfx"></div>
        <div id="footooheader">
        	<div id="footoologo">
            	<a href="#"></a>
            </div>
			<script type="text/javascript">
				var sesuatu = 0;
				function getHTTPObject()
				{
					var xmlhttp;
					if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
						try {
							xmlhttp = new XMLHttpRequest();
						} catch (e) {
							xmlhttp = false;
						}
					}
					return xmlhttp;
				};
				function login ()
				{
					http = getHTTPObject();
					http.open("GET", "php/login.php?xhttp=true", true);
					
					http.onreadystatechange = function()
					{
						if (http.readyState==4 && http.status==200)
						{
							respon = http.responseText;
							var x;
							if (respon.charAt(0) == ' ')
								x = 1;
							else
								x = 2;
							usr = '<?php
								echo $_SESSION['username'];
								?>';
							if (x != sesuatu)
							{
								if (x == 1)
								{
									document.getElementById('footooheader').innerHTML= '<div id="footoologo"><a href="#"></a></div>';
									document.getElementById('footooheader').innerHTML+= '<div id="loginarea"><form action="php/auth.php" method="post"><table id="inputLogin"><tr><td class="iltext">Username</td><td><input type="text" name="usr"></td></tr><tr><td class="iltext">Password</td><td><input type="password" name="pass"></td></tr><tr><td></td><td><input type="submit" value="login" /></td></tr></table></form></div>';
									document.getElementById('menunya').innerHTML= '<li><a class="current" onclick="hohoho()">Welcome..</a></li><li><a onclick="hohoho()">Register..</a></li>';
									document.getElementById('followee').innerHTML= "Login dulu ya...";
									document.getElementById('follower').innerHTML= "Login dulu ya...";
								}
								else
								{
									document.getElementById('footooheader').innerHTML= '<div id="footoologo"><a href="#"></a></div>'+
									'<table class="logouttable"><tr><td class="logouttd"><h1 class="username">'+usr+'</h1><a class="logoutButton" href="php/LogOut.php">Log Out</a></td></tr></table>';
									document.getElementById('menunya').innerHTML= '<li><a href="home.php" class="current">Home</a></li><li><a href="profile.php?'+usr+'">Profile</a></li><li><a href="wall.php?'+usr+'">Wall</a></li><li><a href="fts.php">Photos</a></li><li><a href="message.php">Message</a></li>';
									document.getElementById('follower').innerHTML= follower;
								}
							}
							sesuatu = x;
						}
					};
					http.send();
				}
			</script>
        </div>
        <div id="footoomenu">
            <ul id="menunya">
            </ul>
        </div>
    </div>
    <div id="headermirror">
    	<div id="searchbox">
            <form action="search.php" method="post">
            	<select class="searchselect" name="tipe">
                	<option value="user">User</option>
                	<option value="photo">Photo</option>
                </select>
                <div class="searchbutton">
                    <input id="searchbutton" type="submit" value="" />
                </div>
                <div class="searchtext">
					<input id="searchtext" class="searchtext" type="text" name="searchtext" value="search" onfocus="filledTrue(this.id, 'search')" onblur="filledFalse(this.id, 'search')" />
            	</div>
            </form>
        </div>
	</div>
    <div id="contentwrapper">
    	<div id="contento">
			<div class="box" style="width:430px">yang chatting, yang chatting...</div>
			<table align = "center">
				<tr><td class= "box" valign = "top" colspan=3><form action="php/posting.php" method="post" name="postingan">
				<?php
					echo '<table><tr><td><img src="'.$string1.'" width = 80 height = 80/></td>';
				?>
				<td><textarea cols = 62 rows = 2 name="postingan" id="postingan"></textarea></td></tr><tr>
				<td align="right"><div class="button180" onclick="lanjut()">Post</div></td></tr></table>
				</form></td></tr>
			</table>
			<table align = "center" border = "1" id="tes">
			</table>
				<script type="text/javascript">
					var x=0;
					setInterval ( "update_chat()", 500);
					function update_chat()
					{
						http = getHTTPObject();
						var link = '<?php
						echo "php/submitchat.php?u1=$baris1[0]&u2=$baris2[0]&s1=$string1&s2=$string2"
						?>';
						http.open("GET", link, true);
						
						http.onreadystatechange = function()
						{
							if (http.readyState==4 && http.status==200)
							{
								respon = http.responseText;
								document.getElementById('tes').innerHTML= ""+respon;
							}
						};
						http.send();
					}
					
					function lanjut()
					{
						http = getHTTPObject();
						var link = '<?php
						echo "php/submitchat.php?u1=$baris1[0]&u2=$baris2[0]"
						?>';
						http.open("GET", link+'&teks='+document.getElementById('postingan').value, true);
						http.onreadystatechange = function()
						{
							if (http.readyState==4 && http.status==200)
							{
								document.getElementById('postingan').value = "";
								update_chat();
							}
						};
						http.send();
					}
				</script>
        </div>
	</div>
    
	<div id="footerwrapper">
    	<div id="footer">
            <div id="footerleft"><br/>
            	ft, ft logoes are trademark of ft company. <br/>
            	Copyright 2010 by : <br/>
                13508011 - M. Albadr Lutan Nasution <br/>
                13508019 - Gregorius Ronny Kaluge <br/>
                13508099 - Alwi Alfiansyah Ramdan <br/>
            </div>
            <div id="footermiddle">
            	
            </div>
            <div id="footerright">
            	
            </div>
        </div>
    </div>
</body>
</html>
<?php
		}
	}
?>